﻿#if NETSTANDARD2_0 || NETCOREAPP3_0
using Microsoft.Extensions.DependencyInjection;
#endif
using PetaPoco;
using System;

namespace Longbow.Data
{
    /// <summary>
    /// IDatabase Logger 扩展
    /// </summary>
    public static class DatabaseExtensions
    {
#if NETSTANDARD2_0 || NETCOREAPP3_0
        /// <summary>
        /// IDatabase 实例数据库脚本执行前回调方法
        /// </summary>
        /// <param name="db"></param>
        /// <param name="executedCallback"></param>
        public static void OnCommandExecuted(this IDatabase db, Action<IServiceProvider> executedCallback)
        {
            db.CommandExecuted += (sender, args) => executedCallback?.Invoke(DatabaseServiceCollectionExtensions.ServiceProvider);
        }

        /// <summary>
        /// IDatabase 实例数据库脚本执行后回调方法
        /// </summary>
        /// <param name="db"></param>
        /// <param name="executingCallback"></param>
        public static void OnCommandExecuting(this IDatabase db, Action<IServiceProvider> executingCallback)
        {
            db.CommandExecuting += (sender, args) => executingCallback?.Invoke(DatabaseServiceCollectionExtensions.ServiceProvider);
        }
#else
        /// <summary>
        /// IDatabase 实例数据库脚本执行前回调方法
        /// </summary>
        /// <param name="db"></param>
        /// <param name="executedCallback"></param>
        public static void OnCommandExecuted(this IDatabase db, Action executedCallback)
        {
            db.CommandExecuted += (sender, args) => executedCallback?.Invoke();
        }

        /// <summary>
        /// IDatabase 实例数据库脚本执行后回调方法
        /// </summary>
        /// <param name="db"></param>
        /// <param name="executingCallback"></param>
        public static void OnCommandExecuting(this IDatabase db, Action executingCallback)
        {
            db.CommandExecuting += (sender, args) => executingCallback?.Invoke();
        }
#endif
    }
}
